package jmine.tec.web.wicket.mock;

import java.math.BigDecimal;

import jmine.tec.web.wicket.application.JMineWicketWebApplication;
import jmine.tec.web.wicket.component.autocomplete.NaturalKeyAutoCompleteEntity;
import jmine.tec.web.wicket.component.autocomplete.NaturalKeyAutoCompleteEntityConverter;
import jmine.tec.web.wicket.converter.BigDecimalConverter;
import jmine.tec.web.wicket.converter.BooleanSimNaoConverter;
import jmine.tec.web.wicket.converter.DateConverter;
import jmine.tec.web.wicket.converter.PercentageBigDecimal;
import jmine.tec.web.wicket.converter.PercentageConverter;
import jmine.tec.web.wicket.converter.TecDateConverter;
import jmine.tec.web.wicket.converter.TimestampConverter;
import jmine.tec.web.wicket.pages.main.Home;
import jmine.tec.web.wicket.security.SecureSession;

import org.apache.wicket.Page;
import org.apache.wicket.Request;
import org.apache.wicket.Response;
import org.apache.wicket.Session;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import org.apache.wicket.util.convert.ConverterLocator;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;

import bancosys.tec.utils.date.Date;
import bancosys.tec.utils.date.Timestamp;

/**
 * Wicket web application utilizada nos testes unitarios de telas.
 * 
 * @author Rafael Volpato
 * @date Oct 5, 2011
 */
public class MockWebApplication extends JMineWicketWebApplication {

    private ApplicationContext parentApplicationContext;

    /**
     * Construtor.
     * 
     * @param parentApplicationContext {@link ApplicationContext}
     */
    public MockWebApplication(ApplicationContext parentApplicationContext) {
        this.parentApplicationContext = parentApplicationContext;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Class<? extends Page> getHomePage() {
        return Home.class;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Session newSession(Request request, Response response) {
        return new SecureSession(request);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void init() {
        super.init();

        MockWebApplicationContext webApplicationContext =
                new MockWebApplicationContext(this.parentApplicationContext, this.getServletContext());
        this.addComponentInstantiationListener(new SpringComponentInjector(this, webApplicationContext, true));
        this.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webApplicationContext);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected ConverterLocator newConverterLocator() {
        ConverterLocator converterLocator = new ConverterLocator();
        converterLocator.set(BigDecimal.class, new BigDecimalConverter());
        converterLocator.set(java.util.Date.class, new DateConverter());
        converterLocator.set(Timestamp.class, new TimestampConverter());
        converterLocator.set(Date.class, new TecDateConverter());
        converterLocator.set(PercentageBigDecimal.class, new PercentageConverter());
        converterLocator.set(Boolean.class, new BooleanSimNaoConverter());
        converterLocator.set(NaturalKeyAutoCompleteEntity.class, new NaturalKeyAutoCompleteEntityConverter());
        return converterLocator;
    }
}
